package com.cn.xly2023.controller;

import com.cn.xly2023.pojo.User;
import com.cn.xly2023.service.UserService;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@Api(value = "用户数据test",tags = "用户数据接口API")
@RequestMapping("/user")
public class UserController {

    public UserController() {
        System.out.println("UserController-------------构造函数");
    }

    @Resource
    private UserService userService;

    @RequestMapping(value = "/test01", method = {RequestMethod.POST, RequestMethod.GET, RequestMethod.PUT})
    public String test01() {
        return userService.test01();
    }

    @RequestMapping(value = "/test02", method = {RequestMethod.GET})
    @ApiOperation(value = "用户登录接口",notes = "验证用户的用户名和密码",httpMethod = "GET", response = String.class)
    public String test02(String username) {   // 这里的username可传可不传
        return username;
    }

    @RequestMapping(value = "/test03", method = {RequestMethod.GET})
    public String test03(@RequestParam String username) throws NumberFormatException {   // 这里的username必传，否则抛出错误异常
        Integer.parseInt(username);    // 这个方法的作用是把一个字符串，转换为一个整数数字，如果转换失败，会抛出NumberFormatException
        // 受检异常    非受检异常(运行期异常 RunTimeException的子类)  非受检异常不会强制要求try catch
        return username;
    }

    // 在常规的项目中，所有的查询都是走的get请求，其余增删改，不能使用get请求，至少都是post请求
    // 特殊的查询请求，是不能使用get的，比如登录
    @RequestMapping(value = "/selectById", method = {RequestMethod.GET})
    public Map<String, Object> selectById(@RequestParam Integer id) {
        return userService.selectById(id);
    }

    @RequestMapping(value = "/selectAll", method = {RequestMethod.GET})
    public Map<String, Object> selectAll() {
        return userService.selectAll();
    }

    @RequestMapping(value = "/selectByPage", method = {RequestMethod.GET})
    public Map<String, Object> selectByPage(@RequestParam Integer page,
                                            @RequestParam Integer limit,
                                            String username,
                                            Integer age) {
        return userService.selectByPage(page, limit, username, age);
    }

    @RequestMapping(value = "/insert", method = {RequestMethod.POST})
    public Map<String, Object> insert(@RequestBody User user) {
        return userService.insert(user);
    }

    @RequestMapping(value = "/updateById", method = {RequestMethod.POST})
    public Map<String, Object> updateById(@RequestParam Integer id, @RequestParam Integer age) {
        return userService.updateById(id, age);
    }

    @RequestMapping(value = "/deleteById", method = {RequestMethod.POST})
    public Map<String, Object> deleteById(@RequestParam Integer id) {
        return userService.deleteById(id);
    }

    @RequestMapping(value = "/login", method = {RequestMethod.POST})
    public Map<String, Object> login(@RequestParam String username, @RequestParam String password, HttpServletRequest request) {
//        if(request.getRemoteAddr().equals("172.16.110.56")) {
//            Map<String, Object> hashMap = new HashMap<>();
//            hashMap.put("code", 250);
//            hashMap.put("msg", "对不起，您的IP出现异常，已被系统锁定，请联系对应管理员解锁");
//            return hashMap;
//        }
        System.out.println(request.getRemoteAddr());
        System.out.println(request.getLocalAddr());
        return userService.login(username, password);
    }

    public static void main(String[] args) {
        char c = '国';   // ascii码  a 97   A 65   0 48  最多只能存3位 999极限         unicode码  五位数  99999极限
        int i = 20014;
        c = (char)i;  // 丮
        System.out.println(c);
    }
}


